package com.qq.weixin.mp.message.hxexp.service.impl;

import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hxexp.weixin.constant.StringConstant;
import com.qq.weixin.common.security.WeixinSignatureUtil;
import com.qq.weixin.constant.CompiledPattern;
import com.qq.weixin.mp.message.hxexp.service.ClickEventService;
import com.qq.weixin.mp.message.hxexp.service.MessageService;
import com.qq.weixin.mp.message.hxexp.service.SubscribeEventService;
import com.qq.weixin.mp.message.hxexp.service.TextMessageService;
/**
 * 微信服务器消息服务实现类
 * @author li.jiang
 *
 */
@Service
public final class MessageServiceImpl implements MessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageServiceImpl.class);
    @Autowired
    private TextMessageService textMessageService;
    @Autowired
    private SubscribeEventService subscribeEventService;
    @Autowired
    private ClickEventService clickEventService;
    @Override
    public String messaging(String xmlMsg) throws JAXBException, IOException {
        String replyXml = handleMessage(xmlMsg);
        replyXml = StringConstant.BLANK.equals(replyXml) ? handleEvent(xmlMsg) : replyXml;

        LOGGER.debug("明文回复内容:\n{}", replyXml);
        return replyXml;
    }

    /* (non-Javadoc)
     * @see com.hxexp.weixin.pubac.reply.service.MessageService#verifyURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public String verifyURL(String signature, String token, String timestamp, String nonce, String echostr) {
        try {
            if (WeixinSignatureUtil.checkSignature(signature, token, timestamp, nonce)) {
                LOGGER.debug("服务器URL验证通过");
                if (!StringUtils.isEmpty(echostr)) {
                    return echostr;
                } else {
                    return StringConstant.SUCCESS;
                }
            } else {
                LOGGER.debug("服务器URL验证未通过");
                return StringConstant.BLANK;
            }
        } catch (Exception ex) {
            LOGGER.error("验证URL时发生异常！", ex);
            return StringConstant.BLANK;
        }
    }

    // 处理消息
    private String handleMessage(String xmlMsg) throws JAXBException, IOException {
        String replyXml = StringConstant.BLANK;

        if (CompiledPattern.MSG_TEXT.matcher(xmlMsg).matches()) {
            replyXml = textMessageService.receiveAndReply(xmlMsg);
            // 暂时屏蔽文本消息回复
            // replyXml = StringConstant.BLANK; // NOSONAR
        } else if (CompiledPattern.MSG_IMAGE.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.MSG_VOICE.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.MSG_VIDEO.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.MSG_SHORTVIDEO.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.MSG_LOCATION.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.MSG_LINK.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        }

        return replyXml;
    }

    // 处理事件
    private String handleEvent(String xmlMsg) throws JAXBException, IOException {
        String replyXml = StringConstant.BLANK;

        if (CompiledPattern.EVENT_SUBSCRIBE.matcher(xmlMsg).matches()) {
            replyXml = subscribeEventService.receiveAndReply(xmlMsg);
        } else if (CompiledPattern.EVENT_UNSUBSCRIBE.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.EVENT_SCAN.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.EVENT_UNSUBSCRIBE_SCAN.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.EVENT_LOCATION.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        } else if (CompiledPattern.EVENT_CLICK.matcher(xmlMsg).matches()) {
            replyXml = clickEventService.receiveAndReply(xmlMsg);
        } else if (CompiledPattern.EVENT_VIEW.matcher(xmlMsg).matches()) {
            // TODO // NOSONAR
        }

        return replyXml;
    }
}
